package com.zs.admin.service.wisdbook.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zs.admin.service.wisdbook.IWisdbookDirService;
import com.zs.admin.validate.wisdbook.WisdbookDirCreateValidate;
import com.zs.admin.validate.wisdbook.WisdbookDirSearchValidate;
import com.zs.admin.validate.wisdbook.WisdbookDirUpdateValidate;
import com.zs.admin.validate.commons.IdValidate;
import com.zs.admin.vo.wisdbook.WisdbookDirDetailVo;
import com.zs.admin.vo.wisdbook.WisdbookDirListedVo;
import com.zs.common.entity.wisdbook.WisdbookDir;
import com.zs.common.mapper.WisdbookDirMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 智书目录实现类
 * @author SwAdmin
 */
@Service
public class WisdbookDirServiceImpl implements IWisdbookDirService {
        
    @Resource
    WisdbookDirMapper wisdbookDirMapper;

    /**
     * 智书目录列表
     *
     * @author SwAdmin
     * @param searchValidate 搜索参数
     * @return PageResult<WisdbookDirListedVo>
     */
    @Override
    public List<WisdbookDirListedVo> list(WisdbookDirSearchValidate searchValidate) {

        QueryWrapper<WisdbookDir> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByAsc(Arrays.asList("sort", "id"));

        wisdbookDirMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:pid:int",
            "=:text:str",
            "=:bookId@book_id:int",
            "=:pageIds@page_ids:str",
            "=:sort:int",
        });

        List<WisdbookDir> res = wisdbookDirMapper.selectList(queryWrapper);
        List<WisdbookDirListedVo> list = new ArrayList<>();
        for (int i = 0; i < res.size(); i++) {
            WisdbookDir model = res.get(i);
            WisdbookDirListedVo k = new WisdbookDirListedVo();
            BeanUtils.copyProperties(model, k);
            list.add(k);
        }
        List<WisdbookDirListedVo> rlist = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            WisdbookDirListedVo k = list.get(i);
            if(k.getPid()==null || k.getPid()==0){
                List<WisdbookDirListedVo> child = getDirChild(list, k.getId());
                if(child.size()>0){
                    k.setChildren(child);
                }else{
                    k.setChildren(new ArrayList<WisdbookDirListedVo>());
                }
                rlist.add(k);
            }
        }
        return rlist;
    }

    /**
     * 循环遍历目录树
     * @param list
     * @param pid
     * @return
     */
    private List<WisdbookDirListedVo> getDirChild(List<WisdbookDirListedVo> list,int pid){
        List<WisdbookDirListedVo> array = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            WisdbookDirListedVo k = list.get(i);
            if(k.getPid()!=null && k.getPid()==pid){
                List<WisdbookDirListedVo> child = getDirChild(list, k.getId());
                if(child.size()>0){
                    k.setChildren(child);
                }else{
                    k.setChildren(new ArrayList<WisdbookDirListedVo>());
                }
                array.add(k);
            }
        }
        return array;
    }

    /**
     * 智书目录详情
     *
     * @author SwAdmin
     * @param idValidate 主键参数
     * @return WisdbookDir
     */
    @Override
    public WisdbookDirDetailVo detail(IdValidate idValidate) {
        WisdbookDir model = wisdbookDirMapper.selectOne(
                new QueryWrapper<WisdbookDir>()
                    .eq("id", idValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        WisdbookDirDetailVo vo = new WisdbookDirDetailVo();
        BeanUtils.copyProperties(model, vo);
        vo.setChildren(new ArrayList<WisdbookDirDetailVo>());
        return vo;
    }

    /**
     * 智书目录新增
     *
     * @author SwAdmin
     * @param createValidate 参数
     */
    @Override
    public WisdbookDirDetailVo add(WisdbookDirCreateValidate createValidate) {
        WisdbookDir model = new WisdbookDir();
        model.setPid(createValidate.getPid());
        model.setText(createValidate.getText());
        model.setBookId(createValidate.getBookId());
        model.setPageIds(createValidate.getPageIds());
        model.setSort(createValidate.getSort());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        wisdbookDirMapper.insert(model);
        WisdbookDirDetailVo vo = new WisdbookDirDetailVo();
        BeanUtils.copyProperties(model, vo);
        vo.setChildren(new ArrayList<WisdbookDirDetailVo>());
        return vo;
    }

    /**
     * 智书目录编辑
     *
     * @author SwAdmin
     * @param updateValidateList 参数
     */
    @Override
    public void edit(List<WisdbookDirUpdateValidate> updateValidateList) {
        for (int i = 0; i < updateValidateList.size(); i++) {
            WisdbookDirUpdateValidate updateValidate = updateValidateList.get(i);
            WisdbookDir model = wisdbookDirMapper.selectOne(
                    new QueryWrapper<WisdbookDir>()
                            .eq("id",  updateValidate.getId())
                            .eq("is_delete", 0)
                            .last("limit 1"));
            if(model!=null) {
                model.setId(updateValidate.getId());
                model.setPid(updateValidate.getPid());
                model.setText(updateValidate.getText());
                model.setBookId(updateValidate.getBookId());
                model.setPageIds(updateValidate.getPageIds());
                model.setSort(updateValidate.getSort());
                model.setUpdateTime(System.currentTimeMillis() / 1000);
                wisdbookDirMapper.updateById(model);
            }
        }
    }

    /**
     * 智书目录删除
     *
     * @author SwAdmin
     * @param idValidateList 主键ID
     */
    @Override
    public void del(List<IdValidate> idValidateList) {
        for (int i = 0; i < idValidateList.size(); i++) {
            IdValidate idValidate = idValidateList.get(i);
            WisdbookDir model = wisdbookDirMapper.selectOne(
                    new QueryWrapper<WisdbookDir>()
                            .eq("id", idValidate.getId())
                            .eq("is_delete", 0)
                            .last("limit 1"));
            if(model!=null) {
                model.setIsDelete(1);
                model.setDeleteTime(System.currentTimeMillis() / 1000);
                wisdbookDirMapper.updateById(model);
                delChild(model);
            }
        }
    }

    private void delChild(WisdbookDir wisdbookDir) {
            List<WisdbookDir> list = wisdbookDirMapper.selectList(
                    new QueryWrapper<WisdbookDir>()
                            .eq("pid", wisdbookDir.getId())
                            .eq("is_delete", 0));
        for (int i = 0; i < list.size(); i++) {
            WisdbookDir model = list.get(i);
            if(model!=null) {
                model.setIsDelete(1);
                model.setDeleteTime(System.currentTimeMillis() / 1000);
                wisdbookDirMapper.updateById(model);
                delChild(model);
            }
        }

    }

}
